/**
 * 表格公共model,
 * 后端api， url判断当前路由URL
 */
import * as api from '../service'
const grid = {
  namespaced: true,
  state: {
      data: {},
      keyword: null,
      fetching: false
  },
  mutations: {
      SET_DATA: (state, { data }) => {
          state.data = data
      },
      SET_FETCHING:(state, payload) => {
          state.fetching = payload
      }
  },
  actions: {
      async fetch({ commit, state }, payload) {
          commit('SET_FETCHING', true)
          const params = {
              current: state.current,
              size: state.size,
              ...payload
          }
          const response = await api.fetch(params)
          if (response && response.httpCode === 200) {
              const { total, current, size, data} = response;
              commit('SET_DATA', { data, total, current, size })
          }
          commit('SET_FETCHING', false)
      },
      async get({ commit }, payload) {
        commit('SET_FETCHING', true)
        const response = await api.get(payload)
        if(response) {
          const { data = {} } = response;
          delete data.ctime
          delete data.mtime
          delete data.available
          delete data.createId
          commit('SET_DATA', { data })
        }
        commit('SET_FETCHING', false)
      },
      async save({ commit }, payload) {
          return await api.create(payload)
      },
      async update({ commit }, payload) {
          return await api.update(payload)
      },
      async remove({ commit }, payload) {
          return await api.remove(payload)
      }
  }
}

export default grid;
